package com.jtmproject.classes;

import com.jtmproject.GameActivity;
import com.jtmproject.JoinServer;
import com.jtmproject.PongGame;
import com.jtmproject.bluetooth.Client;

import android.content.Intent;
import android.os.AsyncTask;

/**
 * @author Javier Tejedor
 */ 
public class TaskConnectWithServer extends AsyncTask<String, String, String> {

	private JoinServer mainActivity;
	private Client client;
	
	/**
	 * constructor
	 * @param mainActivity
	 */
	public TaskConnectWithServer(JoinServer mainActivity, Client client){
		this.mainActivity = mainActivity;
		this.client = client;
	}
	
	/* (non-Javadoc)
	 * @see android.os.AsyncTask#doInBackground(java.lang.Object[])
	 */
	@Override
	protected String doInBackground(String... params) {
		
		while(!client.isConnected()){
			
			client.sendMessage("start_game");
			
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
	 */
	@Override
	protected void onPostExecute(String result) {
		Intent intent = new Intent(mainActivity, GameActivity.class);
        intent.putExtra("type_gamer", PongGame.CLIENT);
		mainActivity.startActivity(intent);
        
		mainActivity.finish();
		super.onPostExecute(result);
	}

}
